package gwtappcontainer.server.apis.admin;

import static com.googlecode.objectify.ObjectifyService.ofy;
import gwtappcontainer.server.DatastoreNamespaces;
import gwtappcontainer.server.apps.APIException;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apis.admin.RoleProp;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.NamespaceManager;
import com.googlecode.objectify.ObjectifyService;

class RoleRepository {
	
	static {		
		ObjectifyService.factory().register(RoleEntity.class);		
	}	
	
	private final String NAMESPACE = DatastoreNamespaces.ADMIN; 
			
	RoleProp addRole(String name) {
						
		name = name.toUpperCase();
		//name should not be already present
		RoleEntity entity = getRoleEntity(name);
		
		if (null != entity) 
			throw new APIException(Status.ERROR_RESOURCE_ALREADY_EXISTS, 
					"Role [" + name + "] is already present");
		
		
		entity = new RoleEntity();
		entity.name = name;
		
		NamespaceManager.set(NAMESPACE);
		ofy().save().entity(entity).now();
		
		return entity.toProp();				
	}

	ArrayList<RoleProp> getAllRoles() {
		
		NamespaceManager.set(NAMESPACE);
		
		List<RoleEntity> entities = ofy().load().type(RoleEntity.class).list();		
		ArrayList<RoleProp> props = new ArrayList<RoleProp>();
		
		for (RoleEntity e : entities) {
			props.add(e.toProp());
		}
		
		return props;
	}	
		
	
	RoleProp updateRole(String existingName, String newName) {
		
		NamespaceManager.set(NAMESPACE);
		
		RoleEntity entity = getRoleEntity(existingName);		
		
		if (null == entity) {	
			throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST, 
					"Role [" + existingName + "] does not exist");			
		}	
								
		//if new name is different - check if it is valid
		existingName = existingName.toUpperCase();
		newName = newName.toUpperCase();
		if (! existingName.equals(newName)) {
			RoleEntity entity2 = getRoleEntity(newName);

			if (null != entity2) {			
				throw new APIException(Status.ERROR_RESOURCE_ALREADY_EXISTS, 
						"Role [" + newName + "] already exists.");							
			}
		}
		
		//save with new name
		entity.name = newName;
		ofy().save().entity(entity).now();
		
		return entity.toProp();				
	}
	
	void deleteRole(String name) {
		NamespaceManager.set(NAMESPACE);
		
		RoleEntity entity = getRoleEntity(name);
		
		if (null == entity) {
			throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST, 
					"Role [" + name + "] does not exist");			
		}
										
		ofy().delete().entity(entity).now();		
	}
	
	@SuppressWarnings("unused")
	private long getRoleId(String name) {
					
		List<RoleProp> all = getAllRoles();
		
		name = name.toLowerCase();
		long id = 0;
		for (RoleProp prop : all) {
			if (prop.name.toLowerCase().equals(name))
				id = prop.id;
		}
				
		return id;			
	}	
	
	@SuppressWarnings("unused")
	private RoleProp getRole(long id) {
		NamespaceManager.set(NAMESPACE);
		
		RoleEntity entity = ofy().load().type(RoleEntity.class).id(id).get();
		
		RoleProp prop = null;
		if (null != entity) 
			prop = entity.toProp();
		
		return prop;
	}
	
	RoleEntity getRoleEntity(String name) {
		NamespaceManager.set(NAMESPACE);
		
		List<RoleEntity> all = ofy().load().type(RoleEntity.class).list();
		
		name = name.toUpperCase();
		for (RoleEntity roleEntity : all) {
			if (roleEntity.name.equals(name)) {
				return roleEntity;
			}
		}
		
		return null;	
	}
}
